/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking;

import java.io.IOException;

import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap.Positive;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.filter.Matcher;
import edu.umd.cs.findbugs.xml.XMLOutput;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class PositiveMatchers {
	private static UserDesignationToPositivesMap sharedMapper = new UserDesignationToPositivesMap();

	public static final Matcher FALSE_POSITIVE_MATCHER = new PositiveDesignationMatcher(Positive.FALSE_POSITIVE,
			sharedMapper),
			TRUE_POSITIVE_MATCHER = new PositiveDesignationMatcher(Positive.TRUE_POSITIVE, sharedMapper),
			NEUTRAL_MATCHER = new PositiveDesignationMatcher(Positive.NEUTRAL, sharedMapper);

	private static class PositiveDesignationMatcher implements Matcher {

		private final Positive positiveValueToMatch;
		private final UserDesignationToPositivesMap mapper;

		public PositiveDesignationMatcher(Positive positiveValueToMatch, UserDesignationToPositivesMap mapper) {
			this.positiveValueToMatch = positiveValueToMatch;
			this.mapper = mapper;
		}

		public boolean match(BugInstance bugInstance) {
			Positive positiveValueFromBug = mapper.getPositiveDesignationFromBug(bugInstance);
			return positiveValueFromBug == positiveValueToMatch;
		}

		public void writeXML(XMLOutput xmlOutput, boolean disabled) throws IOException {
			// Until the matcher is used to filter bug reports, I don't believe this is necessary
			throw new IOException(new UnsupportedOperationException("Not yet implemented."));
		}

	}

}
